package com.zjy.weather.adapter

import android.content.Context
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import com.zjy.weather.R
import com.zjy.weather.bean.Weather
import com.zjy.weather.view.LineData
import com.zjy.weather.view.LineView
import java.text.SimpleDateFormat

/**
 * Created by zhongjy on 2017/11/3.
 * Copyright (C) 2017 zhongjy. All rights reserved.
 */
class ChangeViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
    var tempChange: TextView
    var tempTips: TextView
    var windChange: TextView
    var windTips: TextView

    init {
        tempChange = itemView.findViewById(R.id.temp_change_tv) as TextView
        tempTips = itemView.findViewById(R.id.temp_tips) as TextView
        windChange = itemView.findViewById(R.id.wind_change_tv) as TextView
        windTips = itemView.findViewById(R.id.wind_tips) as TextView
    }
}

class TodayViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
    var tempNow: TextView
    var tempRange: TextView
    var weatherDesc: TextView
    var windDesc: TextView
    var humIndex: TextView
    var weatherIcon: ImageView

    init {
        tempNow = itemView.findViewById(R.id.temp_now_tv) as TextView
        tempRange = itemView.findViewById(R.id.temp_range_tv) as TextView
        weatherDesc = itemView.findViewById(R.id.weather_desc_tv) as TextView
        windDesc = itemView.findViewById(R.id.wind_tv) as TextView
        humIndex = itemView.findViewById(R.id.hum_tv) as TextView
        weatherIcon = itemView.findViewById(R.id.weather_img) as ImageView
    }
}

class TodayOtherViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
    var pcpn: TextView
    var pop: TextView
    var pres: TextView
    var vis: TextView
    var cloud: TextView
    var uv: TextView

    init {
        pcpn = itemView.findViewById(R.id.pcpn_tv) as TextView
        pop = itemView.findViewById(R.id.pop_tv) as TextView
        pres = itemView.findViewById(R.id.pres_tv) as TextView
        vis = itemView.findViewById(R.id.vis_tv) as TextView
        cloud = itemView.findViewById(R.id.cloud_tv) as TextView
        uv = itemView.findViewById(R.id.uv_tv) as TextView
    }
}

class LifeViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
    var life: TextView

    init {
        life = itemView.findViewById(R.id.life) as TextView
    }
}

class LineViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
    var title: TextView
    var line: LineView

    init {
        title = itemView.findViewById(R.id.title_tv) as TextView
        line = itemView.findViewById(R.id.line_lv) as LineView
    }
}

class WeatherAdapter(context: Context, weather: Weather?) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
    val WEATHER_CHANGE_TYPE = 0
    val WEATHER_TODAT_TYPE = 1
    val WEATHER_TODAT_OTHER_TYPE = 2
    val WEATHER_LIFE_TYPE = 3
    val WEATHER_TEMP_LINE_TYPE = 4
    val WEATHER_PCPN_LINE_TYPE = 5
    val WEATHER_HUM_LINE_TYPE = 6
    var mContext: Context
    var mWeather: Weather?

    init {
        mContext = context
        mWeather = weather ?: null
    }

    fun setData(weather: Weather) {
        mWeather = weather
        notifyDataSetChanged()
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder? {
        var inflater: LayoutInflater = LayoutInflater.from(mContext)
        var holder: RecyclerView.ViewHolder? = null
        var view: View
        when (viewType) {
            WEATHER_CHANGE_TYPE -> {
                view = inflater.inflate(R.layout.item_weaher_compare, parent, false);
                holder = ChangeViewHolder(view)
            }
            WEATHER_TODAT_TYPE -> {
                view = inflater.inflate(R.layout.item_weaher_today, parent, false)
                holder = TodayViewHolder(view)
            }
            WEATHER_TODAT_OTHER_TYPE -> {
                view = inflater.inflate(R.layout.item_weaher_today_other, parent, false)
                holder = TodayOtherViewHolder(view)
            }
            WEATHER_LIFE_TYPE -> {
                view = inflater.inflate(R.layout.item_weaher_life, parent, false)
                holder = LifeViewHolder(view)
            }
            WEATHER_TEMP_LINE_TYPE -> {
                view = inflater.inflate(R.layout.item_weaher_line, parent, false)
                holder = LineViewHolder(view)
            }
            WEATHER_PCPN_LINE_TYPE -> {
                view = inflater.inflate(R.layout.item_weaher_line, parent, false)
                holder = LineViewHolder(view)
            }
            WEATHER_HUM_LINE_TYPE -> {
                view = inflater.inflate(R.layout.item_weaher_line, parent, false)
                holder = LineViewHolder(view)
            }
        }
        return holder;

    }

    override fun getItemViewType(position: Int): Int {
        var type = 0
        when (position) {
            0 -> type = WEATHER_CHANGE_TYPE
            1 -> type = WEATHER_TODAT_TYPE
            2 -> type = WEATHER_TODAT_OTHER_TYPE
            3 -> type = WEATHER_TEMP_LINE_TYPE
            4 -> type = WEATHER_PCPN_LINE_TYPE
            5 -> type = WEATHER_HUM_LINE_TYPE
            6 -> type = WEATHER_LIFE_TYPE
        }
        return type
    }

    fun getResId(name: String): Int {
        return mContext.getResources().getIdentifier("weather_" + name, "drawable", mContext.packageName)
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {

        when (getItemViewType(position)) {
            WEATHER_CHANGE_TYPE -> {
                var holder: ChangeViewHolder = holder as ChangeViewHolder;
                mWeather?.let {
                    var list = it.daily
                    if (list != null && list.size != 0) {
                        var todayWeahter = list.get(0)
                        var tomorrowWeather = list.get(1)

                        var todayWind = 0
                        var todayWindList: List<String>? = todayWeahter.wind_sc?.replace("微风", "1")?.split("-")
                        if (todayWindList != null && todayWindList.size != 0) {
                            todayWind = todayWindList.get(todayWindList.size - 1).toInt()
                        }
                        var tomorrowWind = 0;
                        var tomorrowWindList: List<String>? = tomorrowWeather.wind_sc?.replace("微风", "1")?.split("-")
                        if (tomorrowWindList != null && tomorrowWindList.size != 0) {
                            tomorrowWind = tomorrowWindList.get(tomorrowWindList.size - 1).toInt()
                        }
                        var windChange = "风力:$todayWind->$tomorrowWind"
                        var windChangeTips = if (tomorrowWind > 5) "明天风较大" else "明天风力较小"
                        holder.windChange.text = windChange
                        holder.windTips.text = windChangeTips

                        var todayTemp = (todayWeahter.tmp_max + todayWeahter.tmp_min) / 2
                        var tomorrowTemp = (tomorrowWeather.tmp_max + tomorrowWeather.tmp_min) / 2
                        var tempChange = "温度:$todayTemp℃->$tomorrowTemp℃"
                        var tempChangeTips: String = when {
                            tomorrowTemp > 40 -> "明天要中暑了"
                            tomorrowTemp > 30 -> "明天炎热的天气啊"
                            tomorrowTemp > 20 -> "明天舒服的天气哦"
                            tomorrowTemp > 10 -> "明天有点凉"
                            tomorrowTemp > 0 -> "明天冷啊"
                            tomorrowTemp > -10 -> "明天非常冷啊"
                            else -> {
                                "温度异常"
                            }
                        }
                        holder.tempChange.text = tempChange
                        holder.tempTips.text = tempChangeTips
                    }

                }
            }
            WEATHER_TODAT_TYPE -> {
                var holder: TodayViewHolder = holder as TodayViewHolder;
                mWeather?.let {
                    it.weatherNow?.let {
                        holder.tempNow.text = it.fl.toString() + "℃"
                        holder.humIndex.text = it.hum.toString()
                        holder.weatherDesc.text = it.cond_txt
                        holder.weatherIcon.setImageResource(getResId(it.cond_code.toString()))
                        holder.windDesc.text = "${it.wind_dir} ${it.wind_sc}"
                    }
                    var list = it.daily
                    if (list != null && list.size != 0) {
                        var todayWeahter = list.get(0)
                        holder.tempRange.text = "${todayWeahter.tmp_min}/${todayWeahter.tmp_max} ℃"
                    }
                }
            }
            WEATHER_TODAT_OTHER_TYPE -> {
                var holder: TodayOtherViewHolder = holder as TodayOtherViewHolder
                mWeather?.let {
                    var list = it.daily
                    if (list != null && list.size != 0) {
                        var todayWeahter = list.get(0)
                        todayWeahter?.let {
                            holder.pop.text = "${it.pop}%"
                            holder.pcpn.text = it.pcpn
                            holder.vis.text = it.vis.toString()
                            holder.uv.text = it.uv_index.toString()
                        }
                    }
                    it.weatherNow?.let {
                        holder.cloud.text = it.cloud.toString()
                        holder.pres.text = it.pres.toString()
                    }
                }
            }
            WEATHER_LIFE_TYPE -> {
                var holder: LifeViewHolder = holder as LifeViewHolder
                mWeather?.let {
                    var list = it.lifeStyle
                    if (list != null && list.size != 0) {
                        var buffer = StringBuffer()
                        for (item in list) {
                            buffer.append("\u3000\u3000${item.txt}\n")
                        }
                        holder.life.text = buffer.toString()
                    }
                }
            }
            WEATHER_TEMP_LINE_TYPE -> {
                var holder: LineViewHolder = holder as LineViewHolder

                mWeather?.let {
                    var list = it.daily
                    if (list != null && list.size != 0) {
                        holder.title.text = "${if (list.size > 4) 5 else list.size}日温度"
                        var lineData = ArrayList<LineData>()
                        var format1 = SimpleDateFormat("yyyy-MM-dd")
                        var format2 = SimpleDateFormat("MM-dd")
                        for (item in list) {
                            var date = format2.format(format1.parse(item.date))
                            var temp = ((item.tmp_max + item.tmp_min) / 2).toFloat()
                            date?.let {
                                lineData.add(LineData(date, temp))
                            }
                            if (lineData.size > 4) {
                                break
                            }
                        }
                        if (lineData.size != 0) {
                            holder.line.setData(lineData,"℃")
                        }
                    } else {
                        holder.title.text = "温度变化"
                    }
                }
            }
            WEATHER_PCPN_LINE_TYPE -> {
                var holder: LineViewHolder = holder as LineViewHolder

                mWeather?.let {
                    var list = it.daily
                    if (list != null && list.size != 0) {
                        holder.title.text = "${if (list.size > 4) 5 else list.size}日降水量"
                        var lineData = ArrayList<LineData>()
                        var format1 = SimpleDateFormat("yyyy-MM-dd")
                        var format2 = SimpleDateFormat("MM-dd")
                        for (item in list) {
                            var date = format2.format(format1.parse(item.date))
                            var pcpn = item.pcpn?.toFloat()?:0f
                            date?.let {
                                lineData.add(LineData(date, pcpn))
                            }
                            if (lineData.size > 4) {
                                break
                            }
                        }
                        if (lineData.size != 0) {
                            holder.line.setData(lineData,null)
                        }
                    } else {
                        holder.title.text = "降水量"
                    }
                }
            }
            WEATHER_HUM_LINE_TYPE -> {
                var holder: LineViewHolder = holder as LineViewHolder

                mWeather?.let {
                    var list = it.daily
                    if (list != null && list.size != 0) {
                        holder.title.text = "${if (list.size > 4) 5 else list.size}日湿度"
                        var lineData = ArrayList<LineData>()
                        var format1 = SimpleDateFormat("yyyy-MM-dd")
                        var format2 = SimpleDateFormat("MM-dd")
                        for (item in list) {
                            var date = format2.format(format1.parse(item.date))
                            var pcpn = item.hum.toFloat()
                            date?.let {
                                lineData.add(LineData(date, pcpn))
                            }
                            if (lineData.size > 4) {
                                break
                            }
                        }
                        if (lineData.size != 0) {
                            holder.line.setData(lineData,"%")
                        }
                    } else {
                        holder.title.text = "湿度"
                    }
                }
            }
        }

    }

    override fun getItemCount(): Int {
        return if (mWeather == null) 0 else 7
    }

}